package com.wego.controller.manager;

import com.wego.controller.manager.base.BaseController;
import com.wego.service.base.BaseService;
import com.wego.common.bean.EntryBean;
import com.wego.common.bean.PageBean;
import com.wego.common.bean.ResultBean;
import com.wego.common.enumeration.ResultEnum;
import com.wego.common.minio.MinioUtil;
import com.wego.common.utils.QueryUtil;
import com.wego.common.utils.ResultBeanUtil;
import com.wego.converter.CategoryConverter;
import com.wego.entity.domain.Category;
import com.wego.entity.query.CategoryQuery;
import com.wego.service.CategoryService;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 商品类别控制器
 * @author: hc
 * @date: 2023/7/10
 */
@Controller
@RequestMapping("/manager/category")
public class CategoryController extends BaseController<Category> {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private CategoryConverter categoryConverter;

    @Autowired
    private MinioUtil minioUtil;

    /**
     * 不提供默认的构造方法，限制其子类必须调用该方法为baseService赋值
     * @param service
     */
    public CategoryController(BaseService<Category> service) {
        super(service);
    }

    /**
     * 查找某一类别下面的所有子类
     * @param pid
     * @return
     */
    @ResponseBody
    @GetMapping("/selectEntryByPid")
    List<EntryBean<String>> selectEntryByPid(Long pid) {
        final List<Category> categoryList = categoryService.selectCategoryByPid(pid, -1, false);
        final List<EntryBean<String>> entryBeanList = categoryConverter.categoryList2EntryList(categoryList);
        return entryBeanList;
    }

    @GetMapping("/list")
    public String list(CategoryQuery categoryQuery, Model model, @ModelAttribute("resultBean") ResultBean resultBean) {
        //构造动态查询条件
        String keywords = categoryQuery.getKeywords();
        if (keywords != null) {
            if (categoryQuery.getName() == null) {
                categoryQuery.setName(keywords);
            }
            if (categoryQuery.getInfo() != null) {
                categoryQuery.setInfo(keywords);
            }
        }

        PageBean<Category> pageBean = categoryService.selectPage(categoryQuery);

        //分页时携带上查询条件
        String params = QueryUtil.genQueryCondition(categoryQuery);
        pageBean.setUrl("manager/category/list?" + params);
        model.addAttribute("pageBean", pageBean);

        //查询条件数据回显时使用
        model.addAttribute("categoryQuery", categoryQuery);

        //增删改操作完返回到列表页面的提示信息
        if (resultBean != null) {
            model.addAttribute("resultBean", resultBean);
        }

        return "manager/category";
    }

    @PostMapping("/doAddUpdate")
    public String doAddUpdate(Category category, MultipartFile iconFile, RedirectAttributes redirectAttributes) {
        if (iconFile != null && iconFile.getSize() > 0) {
            final String picFileName = minioUtil.putObject2(iconFile, "category", null);
            category.setIcon(picFileName);
        }

        category.setUpdateTime(LocalDateTime.now());

        ResultBean resultBean;
        Long id = category.getId();

        if (id != null) {
            //修改
            boolean res = categoryService.updateByPrimaryKeySelective(category);
            resultBean = ResultBeanUtil.build(res, ResultEnum.UPDATE_SUCCESS, ResultEnum.UPDATE_EXCEPTION);
        } else {
            //添加
            category.setCreateTime(LocalDateTime.now());
            boolean res = categoryService.insertSelective(category);
            resultBean = ResultBeanUtil.build(res, ResultEnum.INSERT_SUCCESS, ResultEnum.INSERT_EXCEPTION);
        }

        //返回列表页面时，对话框中显示的提示信息
        redirectAttributes.addFlashAttribute("resultBean", resultBean);

        //重定向
        return "redirect:list";
    }
}
